import {createSlice,createAsyncThunk} from '@reduxjs/toolkit';
const address0 = "0x0000000000000000000000000000000000000000"
const balanceSlice = createSlice({
    name:"balance",
    initialState:{
        TokenWallet:"0",//钱包KWT余额（wei转换，必须字符串）
        TokenExchange:"0",//交易所KWT余额（wei转换，必须字符串）
        EtherWallet:"0",//钱包以太币余额
        EtherExchange:"0",//交易所以太币余额
    },
    reducers: {
        setTokenWallet(state,aciont){
            state.TokenWallet = aciont.payload;
        },
        setTokenExchange(state,aciont){
            state.TokenExchange = aciont.payload;
        },
        setEtherWallet(state,aciont){
            state.EtherWallet = aciont.payload;
        },
        setEtherExchange(state,aciont){
            state.EtherExchange = aciont.payload;
        }
    }
})
export const {setTokenWallet,setTokenExchange,setEtherWallet,setEtherExchange } = balanceSlice.actions
export default balanceSlice.reducer;
export const loadBalanceData = createAsyncThunk(
    "balance/fetchBalanceData",
    async(data,{dispatch})=>{
        // console.log(data)

        const {web3, account,token,exchange} = await data;

        //获取钱包的token
        try{
            const TokenWallet = await token.methods.balanceOf(account).call()
            // console.log("钱包KWT余额：",TokenWallet)
            // console.log("币种：",token.options.address)
            dispatch(setTokenWallet(TokenWallet))
            const TokenExchange = await exchange.methods.balanceOf(token.options.address,account).call()
            // console.log("交易所KWT余额：",TokenExchange)
            dispatch(setTokenExchange(TokenExchange))
            const EtherWallet = await web3.eth.getBalance(account)
            // console.log("钱包ETH余额：",EtherWallet)
            dispatch(setEtherWallet(EtherWallet))
            // console.log("币种：",address0)
            const EtherExchange = await exchange.methods.balanceOf(address0,account).call()
            // console.log("交易所ETH余额：",EtherExchange)
            dispatch(setEtherExchange(EtherExchange))
        }catch (e) {
            console.log(e)
        }

        //获取交易所的token
        //获取钱包ether
    }
)